﻿using System;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.ExtensibleStorage;

namespace Transfer.CrossDocument
{
	internal class ExtensibleStorageManager
	{
		public static void WriteOriginExtensibleStorageToCurve(ModelCurve curve, XYZ ptOrigin)
		{
			Guid guid = new Guid(ExtensibleStorageManager.m_GUID);
			Schema schema = Schema.Lookup(guid);
			if (schema == null)
			{
				SchemaBuilder schemaBuilder = new SchemaBuilder(guid);
				schemaBuilder.SetSchemaName("HY_STANDARDROOM_ORIGIN_ESData");
				schemaBuilder.AddSimpleField("Origin", typeof(XYZ)).SetUnitType(0);
				schema = schemaBuilder.Finish();
			}
			Entity entity = new Entity(schema);
			Field field = schema.GetField("Origin");
			entity.Set<XYZ>(field, ptOrigin, (Autodesk.Revit.DB.DisplayUnitType)6);
			curve.SetEntity(entity);
		}

		public static bool IsOriginMarkCurve(ModelCurve line)
		{
			Schema schema = Schema.Lookup(new Guid(ExtensibleStorageManager.m_GUID));
			Entity entity = line.GetEntity(schema);
			return entity.Schema != null;
		}

		public static bool ReadOriginExtensibleStorageFromCurve(ModelCurve curve, ref XYZ ptOrigin)
		{
			Schema schema = Schema.Lookup(new Guid(ExtensibleStorageManager.m_GUID));
			Entity entity = curve.GetEntity(schema);
			if (entity.Schema == null)
			{
				return false;
			}
			ptOrigin = entity.Get<XYZ>(schema.GetField("Origin"), 6);
			return true;
		}

		private static string m_GUID = "CCD55CAB-1FF8-4FFE-B408-1CDC90ED30C5";
	}
}
